package com.adcc.loadsheet.monitor.util;

import com.adcc.aoc.devLib.common.log.Log;
import org.springframework.util.ResourceUtils;

import java.io.File;

/**
 * 文件工具类
 */
public class FileUtil {

    /**
     * 取得文件路径
     * @param fileName
     * @return
     */
    public static String getPath(String fileName){
        try{
            File file = new File("./resources");
            if(file.isDirectory()){
                return "./resources" + "/" + fileName;
            }else{
                return ResourceUtils.getFile("classpath:").getPath() + "/" + fileName;
            }
        }catch (Exception ex){
            Log.error(FileUtil.class,"getPath() error",ex);
            return null;
        }
    }

    /**
     * 读取文件
     * @param fileName
     * @return
     */
    public static File readFile(String fileName){
        try{
            File file = new File("./resources/" + fileName);
            if(!file.isFile()){
                String strURL = "classpath:" + fileName;
                if(ResourceUtils.isUrl(strURL)){
                    file  = ResourceUtils.getFile(strURL);
                }
            }
            return file;
        }catch (Exception ex){
            Log.error(FileUtil.class,"readFile() error",ex);
            return null;
        }
    }
}
